﻿Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient

Partial Class Catalogo
    Inherits System.Web.UI.Page

    'Instancia de claseProducto
    Dim Equipo As New Equipo

    Dim cn As New SqlConnection(ConnectionStrings("cn").ConnectionString)



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Master.MostrarClienteSession()

        If Not Page.IsPostBack Then

            Dim intOpcion As String = Request("Opt")
            If intOpcion = "0" Or intOpcion Is Nothing Then
                cargartTipo()
                cargarMarca()
                cargarColor()
            Else

                cargartTipo()
                cargarMarca()
                cargarColor()
                cboMarca.SelectedValue = intOpcion
                Dim StrQuery As String = ArmarQuery()
                Dim dt As DataTable = Equipo.ListadoParametro(StrQuery)
                GridView1.DataSource = dt
                Session.Add("dt", dt)
                GridView1.DataBind()

            End If




        End If
    End Sub
    Sub cargartTipo()
        Dim StrQuery As String = "select  ltrim(cod_tipo) cod, ltrim(des_tipo) as descri from tipoequipo"
        Dim dt As DataTable = Equipo.ListadoParametro(StrQuery)
        cboTipo.DataSource = dt
        cboTipo.DataTextField = "descri"
        cboTipo.DataValueField = "cod"
        cboTipo.DataBind()

        cboTipo.Items.Insert(0, "--Todos--")

    End Sub
    Sub cargarColor()
        Dim StrQuery As String = "select  ltrim(cod_color) cod, ltrim(desc_color) as descri from color"
        Dim dt As DataTable = Equipo.ListadoParametro(StrQuery)
        cboColor.DataSource = dt
        cboColor.DataTextField = "descri"
        cboColor.DataValueField = "cod"
        cboColor.DataBind()

        cboColor.Items.Insert(0, "--Todos--")


    End Sub
    Sub cargarMarca()
        Dim StrQuery As String = "select  ltrim(cod_marca) cod, ltrim(Name_marca) as descri from tb_marca"
        Dim dt As DataTable = Equipo.ListadoParametro(StrQuery)
        cboMarca.DataSource = dt
        cboMarca.DataTextField = "descri"
        cboMarca.DataValueField = "cod"
        cboMarca.DataBind()

        cboMarca.Items.Insert(0, "--Todos--")

    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        'Cambio de Página
        GridView1.PageIndex = e.NewPageIndex
        'actualizar
        GridView1.DataSource = CType(Session.Item("dt"), DataTable)
        GridView1.DataBind()
    End Sub

    Protected Sub btnConsulta_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        'listar por su descripcion
        Dim StrQuery As String = ArmarQuery()
        Dim dt As DataTable = Equipo.ListadoParametro(StrQuery)
        GridView1.DataSource = dt
        Session.Add("dt", dt)
        GridView1.DataBind()
    End Sub

    Function ArmarQuery() As String
        Dim StrQuery As String

        Dim var As String = CType(Session.Item("cboMoneda"), String)

        If var = "01" Then

            StrQuery = Opcion01()

        ElseIf var = "02" Then

            StrQuery = Opcion02()

        Else

            StrQuery = Opcion01()

        End If
    
        Return StrQuery

    End Function

    Function Opcion01() As String

        Dim strModelo As String = ""
        Dim StrColor As String = ""
        Dim strPrecio As String = ""
        Dim StrMarca As String = ""
        Dim strOrden As String = ""
        Dim apos As String = "'"
        Dim StrQuery As String = "select e.cod_equipo,m.Name_marca, " & _
                                    "e.Equip_desc, t.des_tipo tipo_equipo, e.precio " & _
                                  "from tb_Equipo e inner Join  " & _
                                       "tb_marca m on " & _
                                       "e.cod_marca=m.cod_marca inner join tipoequipo t on " & _
                                       "e.tipo_equipo = t.cod_tipo inner join colorescantidad cc on " & _
                                       "cc.cod_equipo = e.cod_equipo inner join color co on " & _
                                       "co.cod_color = cc.cod_color " & _
                                            "where cant > 0 "

        ' Validar Modelo

        If cboTipo.SelectedValue.Trim <> "--Todos--" Then
            Dim StrVariable = cboTipo.SelectedValue.Trim
            strModelo = " And t.cod_tipo = " & apos & StrVariable & apos

        End If

        'validar Color

        If cboColor.SelectedValue.Trim <> "--Todos--" Then
            Dim StrVariable = cboColor.SelectedValue.Trim
            StrColor = " And co.cod_color = " & apos & StrVariable & apos

        End If


        ' validar Precio

        If CboPrecio.SelectedValue.Trim = "1" Then
            strPrecio = " And e.precio >= 0 and e.precio <= 200 "

        ElseIf CboPrecio.SelectedValue.Trim = "2" Then
            strPrecio = " And e.precio >= 200 and e.precio <= 500 "

        ElseIf CboPrecio.SelectedValue.Trim = "3" Then
            strPrecio = " And e.precio >= 500 and e.precio <= 1000 "

        ElseIf CboPrecio.SelectedValue.Trim = "4" Then
            strPrecio = " And e.precio >= 1000 and e.precio <= 1500 "

        ElseIf CboPrecio.SelectedValue.Trim = "5" Then
            strPrecio = " And e.precio >= 1500 and e.precio <= 2000 "

        ElseIf CboPrecio.SelectedValue.Trim = "6" Then
            strPrecio = " And e.precio >= 2000 and e.precio <= 3000 "

        ElseIf CboPrecio.SelectedValue.Trim = "7" Then
            strPrecio = " And e.precio >= 3000 and e.precio <= 5000 "

        ElseIf CboPrecio.SelectedValue.Trim = "8" Then
            strPrecio = " And e.precio >= 5000 "

        End If


        ' validar marca

        If cboMarca.SelectedValue.Trim <> "--Todos--" Then
            Dim StrVariable = cboMarca.SelectedValue.Trim
            StrMarca = " And m.cod_marca = " & apos & StrVariable & apos
        End If


        ' orden

        If RdFiltro.SelectedValue.Trim = "01" Then

            strOrden = " order by e.precio Asc "
        Else

            strOrden = " order by e.precio Desc "
        End If
        '"order by e.precio "

        StrQuery = StrQuery & strModelo & StrColor & strPrecio & StrMarca & strOrden

        Return StrQuery

    End Function

    Function Opcion02() As String

        Dim dt As DataTable = Equipo.ListadoParametro(" select  max(fecha) fecha , Tipcambio from TipoCambio group by  Tipcambio ")
        Dim strCambio As String = dt.Rows(0).Item("Tipcambio").ToString

        Dim strModelo As String = ""
        Dim StrColor As String = ""
        Dim strPrecio As String = ""
        Dim StrMarca As String = ""
        Dim strOrden As String = ""
        Dim apos As String = "'"
        Dim StrQuery As String = "select e.cod_equipo,m.Name_marca, " & _
                                    "e.Equip_desc, t.des_tipo tipo_equipo, convert(decimal(15,2),e.precio/" & strCambio & ") precio " & _
                                  " from tb_Equipo e inner Join  " & _
                                       "tb_marca m on " & _
                                       "e.cod_marca=m.cod_marca inner join tipoequipo t on " & _
                                       "e.tipo_equipo = t.cod_tipo inner join colorescantidad cc on " & _
                                       "cc.cod_equipo = e.cod_equipo inner join color co on " & _
                                       "co.cod_color = cc.cod_color " & _
                                            "where cant > 0 "

        ' Validar Modelo

        If cboTipo.SelectedValue.Trim <> "--Todos--" Then
            Dim StrVariable = cboTipo.SelectedValue.Trim
            strModelo = " And t.cod_tipo = " & apos & StrVariable & apos

        End If

        'validar Color

        If cboColor.SelectedValue.Trim <> "--Todos--" Then
            Dim StrVariable = cboColor.SelectedValue.Trim
            StrColor = " And co.cod_color = " & apos & StrVariable & apos

        End If


        ' validar Precio

        If CboPrecio.SelectedValue.Trim = "1" Then
            strPrecio = " And e.precio >= 0 and e.precio <= 200 "

        ElseIf CboPrecio.SelectedValue.Trim = "2" Then
            strPrecio = " And e.precio >= 200 and e.precio <= 500 "

        ElseIf CboPrecio.SelectedValue.Trim = "3" Then
            strPrecio = " And e.precio >= 500 and e.precio <= 1000 "

        ElseIf CboPrecio.SelectedValue.Trim = "4" Then
            strPrecio = " And e.precio >= 1000 and e.precio <= 1500 "

        ElseIf CboPrecio.SelectedValue.Trim = "5" Then
            strPrecio = " And e.precio >= 1500 and e.precio <= 2000 "

        ElseIf CboPrecio.SelectedValue.Trim = "6" Then
            strPrecio = " And e.precio >= 2000 and e.precio <= 3000 "

        ElseIf CboPrecio.SelectedValue.Trim = "7" Then
            strPrecio = " And e.precio >= 3000 and e.precio <= 5000 "

        ElseIf CboPrecio.SelectedValue.Trim = "1" Then
            strPrecio = " And e.precio >= 5000 "

        End If


        ' validar marca

        If cboMarca.SelectedValue.Trim <> "--Todos--" Then
            Dim StrVariable = cboMarca.SelectedValue.Trim
            StrMarca = " And m.cod_marca = " & apos & StrVariable & apos
        End If


        ' orden

        If RdFiltro.SelectedValue.Trim = "01" Then

            strOrden = " order by e.precio Asc "
        Else

            strOrden = " order by e.precio Desc "
        End If
        '"order by e.precio "

        StrQuery = StrQuery & strModelo & StrColor & strPrecio & StrMarca & strOrden

        Return StrQuery

    End Function

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        'indice seleccionado
        Dim f As Integer = GridView1.SelectedIndex

        ' capturo los datos
        Dim cod As String = CType(GridView1.Rows(f).FindControl("lblCodigo"), Label).Text
        Dim mar As String = CType(GridView1.Rows(f).FindControl("lblMarca"), Label).Text
        Dim mo As String = CType(GridView1.Rows(f).FindControl("lblModelo"), Label).Text
        Dim tip As String = CType(GridView1.Rows(f).FindControl("lblTipo"), Label).Text
        Dim precio As Decimal = CType(GridView1.Rows(f).FindControl("lblprecio"), Label).Text

        '  cookie y envio de datos

        Dim datos As New HttpCookie("equipo")
        datos.Values.Add("cod", cod)
        datos.Values.Add("mar", mar)
        datos.Values.Add("mod", mo)
        datos.Values.Add("tip", tip)
        datos.Values.Add("pre", precio)
        Response.Cookies.Add(datos)

        Response.Redirect("CDetallesCelular.aspx")

    End Sub

    


End Class
